package com.kcyu.sfhinvoice.global;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.util.SaResult;
import com.kcyu.sfhinvoice.config.MyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 全局异常拦截
    @ExceptionHandler
    public SaResult handlerException(Exception e) {
        e.printStackTrace();
        log.error("服务执行异常---->{}", e.getMessage());
        return SaResult.error(e.getMessage());
    }

    @ExceptionHandler(value = NotLoginException.class)
    public SaResult handlerException(NotLoginException e) {
        log.error("没有登陆---->{}", e.getMessage());
        return new SaResult(401, e.getMessage(), null);
    }

    @ExceptionHandler(value = SQLException.class)
    public SaResult msgMySQLExecuteError(Exception e) {
        e.printStackTrace();
        log.error("Mysql执行异常");
        String message = e.getMessage();
        return SaResult.error(message);
    }

    @ExceptionHandler(value = MyException.class)
    public SaResult myExceptionExecuteError(Exception e) {
        log.error("自定义报错: {}", e.getMessage());
        String message = e.getMessage();
        return SaResult.error(message);
    }


    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public SaResult msgNotFind(Exception e) {
        e.printStackTrace();
        log.error("请求错误");
        String message = e.getMessage();
        return SaResult.error("请求内容未传递" + message);
    }
}
